package com.rjwh.dingdong.client.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aishua.lib.utils.AsStrUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.bean.localbean.DrawBookData;
import com.rjwh.dingdong.client.bean.localbean.DrawBookTypeList;
import com.rjwh.dingdong.client.util.PicPathUtil;
import com.rjwh.dingdong.client.util.UtilPublic;
import com.rjwh.dingdong.client.util.ViewHolder;
import com.wby.base.AdapterBase;

import java.util.List;

/**
 * 绘本目录列表
 *
 * @author 王小珂
 * @ClassName PictureBookCatalogListAdapter
 * @Description TODO(这里用一句话描述这个类的作用)
 * @date 2016-6-1 下午5:22:57
 * @history 1.YYYY-MM-DD author: description:
 */
public class DrawBookCatalogListAdapter extends AdapterBase<DrawBookTypeList> {
    private LayoutInflater inflater;
    private Context mContext;
    private DrawBookCatalogItemClickListener drawBookCatalogItemClickListener;

    public DrawBookCatalogListAdapter(Context context, DrawBookCatalogItemClickListener drawBookCatalogItemClickListener) {
        this.mContext = context;
        this.drawBookCatalogItemClickListener = drawBookCatalogItemClickListener;
        inflater = LayoutInflater.from(context);
    }

    @Override
    protected View getExView(int arg0, View arg1, ViewGroup arg2) throws Exception {
        if (arg1 == null) {
            arg1 = inflater.inflate(R.layout.draw_book_catalog_item, null);
        }
        LinearLayout bookRoot = ViewHolder.get(arg1, R.id.item_draw_book_catalog_book_ll);
        ImageView tagIV = ViewHolder.get(arg1, R.id.item_draw_book_catalog_tab_img);
        RelativeLayout book1 = ViewHolder.get(arg1, R.id.item_draw_book_catalog_book_rl1);
        RelativeLayout book2 = ViewHolder.get(arg1, R.id.item_draw_book_catalog_book_rl2);
        RelativeLayout book3 = ViewHolder.get(arg1, R.id.item_draw_book_catalog_book_rl3);
        TextView book_title_tv1 = ViewHolder.get(arg1, R.id.item_draw_book_catalog_book_title_tv1);
        TextView book_age_tv1 = ViewHolder.get(arg1, R.id.item_draw_book_catalog_book_age_tv1);
        ImageView book_pic_iv1 = ViewHolder.get(arg1, R.id.item_draw_book_catalog_book_pic_iv1);
        ImageView book_percent_iv1 = ViewHolder.get(arg1, R.id.item_draw_book_catalog_book_percent_iv1);
        TextView book_title_tv2 = ViewHolder.get(arg1, R.id.item_draw_book_catalog_book_title_tv2);
        TextView book_age_tv2 = ViewHolder.get(arg1, R.id.item_draw_book_catalog_book_age_tv2);
        ImageView book_pic_iv2 = ViewHolder.get(arg1, R.id.item_draw_book_catalog_book_pic_iv2);
        ImageView book_percent_iv2 = ViewHolder.get(arg1, R.id.item_draw_book_catalog_book_percent_iv2);
        TextView book_title_tv3 = ViewHolder.get(arg1, R.id.item_draw_book_catalog_book_title_tv3);
        TextView book_age_tv3 = ViewHolder.get(arg1, R.id.item_draw_book_catalog_book_age_tv3);
        ImageView book_pic_iv3 = ViewHolder.get(arg1, R.id.item_draw_book_catalog_book_pic_iv3);
        ImageView book_percent_iv3 = ViewHolder.get(arg1, R.id.item_draw_book_catalog_book_percent_iv3);
        View dividerView = ViewHolder.get(arg1, R.id.item_draw_book_catalog_divider_view);
        DrawBookTypeList drawBookTypeList = mList.get(arg0);
        String typeimage = drawBookTypeList.getTypeimage();
        boolean isFirst = drawBookTypeList.isFirst();
        boolean isLast = drawBookTypeList.isLast();

        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) bookRoot.getLayoutParams();

        if (!AsStrUtils.isEmpty(typeimage)) {
            if (isLast) {
                dividerView.setVisibility(View.VISIBLE);
            } else {
                dividerView.setVisibility(View.INVISIBLE);
            }
            layoutParams.setMargins(UtilPublic.dp2px(45, mContext), UtilPublic.dp2px(20, mContext), 0, 0);

            if (isFirst) {
                tagIV.setVisibility(View.VISIBLE);
                Glide.with(mContext).load(PicPathUtil.getPicUrl(typeimage)).dontAnimate().diskCacheStrategy(DiskCacheStrategy.ALL).into(tagIV);
            } else {
                tagIV.setVisibility(View.GONE);
            }
        } else {
            //绘本未分类
            layoutParams.setMargins(UtilPublic.dp2px(5, mContext), UtilPublic.dp2px(20, mContext), 0, 0);
            tagIV.setVisibility(View.GONE);
        }

        List<DrawBookData> booklist = drawBookTypeList.getBooklist();
        if (booklist == null) {
            return arg1;
        }
        switch (booklist.size()) {
            case 1:
                DrawBookData drawBook = booklist.get(0);
                book1.setVisibility(View.VISIBLE);
                book2.setVisibility(View.INVISIBLE);
                book3.setVisibility(View.INVISIBLE);
                setView(book_title_tv1, book_age_tv1, book_pic_iv1, book_percent_iv1, drawBook);
                book1.setOnClickListener(new MyOnClickListener(drawBook));
                break;
            case 2:
                DrawBookData drawBook1 = booklist.get(0);
                DrawBookData drawBook2 = booklist.get(1);
                book1.setVisibility(View.VISIBLE);
                book2.setVisibility(View.VISIBLE);
                book3.setVisibility(View.INVISIBLE);
                setView(book_title_tv1, book_age_tv1, book_pic_iv1, book_percent_iv1, drawBook1);
                setView(book_title_tv2, book_age_tv2, book_pic_iv2, book_percent_iv2, drawBook2);
                book1.setOnClickListener(new MyOnClickListener(drawBook1));
                book2.setOnClickListener(new MyOnClickListener(drawBook2));
                break;
            case 3:
                DrawBookData drawBook31 = booklist.get(0);
                DrawBookData drawBook32 = booklist.get(1);
                DrawBookData drawBook33 = booklist.get(2);
                book1.setVisibility(View.VISIBLE);
                book2.setVisibility(View.VISIBLE);
                book3.setVisibility(View.VISIBLE);
                setView(book_title_tv1, book_age_tv1, book_pic_iv1, book_percent_iv1, drawBook31);
                setView(book_title_tv2, book_age_tv2, book_pic_iv2, book_percent_iv2, drawBook32);
                setView(book_title_tv3, book_age_tv3, book_pic_iv3, book_percent_iv3, drawBook33);
                book1.setOnClickListener(new MyOnClickListener(drawBook31));
                book2.setOnClickListener(new MyOnClickListener(drawBook32));
                book3.setOnClickListener(new MyOnClickListener(drawBook33));
                break;

            default:
                break;
        }
        return arg1;
    }

    private class MyOnClickListener implements View.OnClickListener {
        private DrawBookData bookData;

        public MyOnClickListener(DrawBookData drawBook) {
            this.bookData = drawBook;
        }

        @Override
        public void onClick(View v) {
            if (drawBookCatalogItemClickListener != null) {
                drawBookCatalogItemClickListener.onDrawBookCatalogItemClick(bookData);
            }
        }
    }

    private void setView(TextView book_title_tv, TextView book_age_tv, ImageView book_pic_iv, ImageView book_percent_iv, DrawBookData drawBook) {
        book_title_tv.setText(drawBook.getBookname());
        book_age_tv.setText(drawBook.getAge());
        String percent = drawBook.getPercent();
        if (AsStrUtils.isEmpty(percent)) {
            book_percent_iv.setVisibility(View.INVISIBLE);
        } else {
            switch (percent) {
                case "0%":
                    book_percent_iv.setVisibility(View.INVISIBLE);
                    break;
                case "17%":
                    book_percent_iv.setVisibility(View.VISIBLE);
                    book_percent_iv.setBackgroundResource(R.drawable.icon_draw_book_catalog_book_percent17);
                    break;
                case "33%":
                    book_percent_iv.setVisibility(View.VISIBLE);
                    book_percent_iv.setBackgroundResource(R.drawable.icon_draw_book_catalog_book_percent33);
                    break;
                case "50%":
                    book_percent_iv.setVisibility(View.VISIBLE);
                    book_percent_iv.setBackgroundResource(R.drawable.icon_draw_book_catalog_book_percent50);
                    break;
                case "67%":
                    book_percent_iv.setVisibility(View.VISIBLE);
                    book_percent_iv.setBackgroundResource(R.drawable.icon_draw_book_catalog_book_percent67);
                    break;
                case "83%":
                    book_percent_iv.setVisibility(View.VISIBLE);
                    book_percent_iv.setBackgroundResource(R.drawable.icon_draw_book_catalog_book_percent83);
                    break;
                case "100%":
                    book_percent_iv.setVisibility(View.VISIBLE);
                    book_percent_iv.setBackgroundResource(R.drawable.icon_draw_book_catalog_book_percent100);
                    break;

                default:
                    break;
            }
        }
        Glide.with(mContext).load(PicPathUtil.getPicUrl(drawBook.getCoverurl())).dontAnimate().placeholder(R.drawable.icon_drawbook_pic_holder).error(R.drawable.icon_drawbook_pic_holder).diskCacheStrategy(DiskCacheStrategy.ALL).into(book_pic_iv);
    }

    @Override
    protected void onReachBottom() {

    }

    public interface DrawBookCatalogItemClickListener {
        void onDrawBookCatalogItemClick(DrawBookData bookData);
    }
}
